package com.hezhiqin.formework.webmvc.servlet;

import java.io.File;
import java.util.Locale;

/**
 * @program: homeWork
 * @description:
 * @author: hezhiqin
 * @create: 2019-11-01 11:48
 */
public class HZQViewResolver {

    private  File  templateRootDir;


    private  final  String DEFAULT_TEMPLATE_SUFFX = "html";


    public HZQViewResolver(String templateRoot) {
        String templateRootPath = this.getClass().getClassLoader().getResource(templateRoot).getFile();
        templateRootDir = new File(templateRootPath);
    }

    public HZQView resolveViewName(String viewName, Locale locale) throws Exception{
        //viewName 解析成文件
        if (viewName==null||"".equals(viewName)){
            return null;
        }
        viewName = viewName.endsWith(DEFAULT_TEMPLATE_SUFFX)?viewName:(viewName+".html");
        File templatefile  = new File((templateRootDir.getPath()+"/"+viewName).replaceAll("/+","/"));
        return new HZQView(templatefile);
    }

    public File getTemplateRootDir() {
        return templateRootDir;
    }

    public void setTemplateRootDir(File templateRootDir) {
        this.templateRootDir = templateRootDir;
    }


}
